package com.xiaohong.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.goods.pojo.Category;
import com.xiaohong.goods.pojo.CategoryTree;
import com.xiaohong.goods.service.CategoryService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "Category接口文档")
@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /***
     * 分页条件搜索实现Category
     * @param category
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询Category",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Category对象",required = true) Category category, @PathVariable int page, @PathVariable  int size){
        //调用CategoryService实现分页条件查询Category
        Page pageInfo = categoryService.findPage(category, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现Category
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询Category",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用CategoryService实现分页查询Category
        Page pageInfo = categoryService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索Category数据
     * @param category
     * @return
     */
    @Operation(summary = "多条件查询Category",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<Category>> findList(@RequestBody(required = false) @Parameter(name = "Category对象",required = true) Category category){
        //调用CategoryService实现条件查询Category
        List<Category> list = categoryService.findList(category);
        return new Result<List<Category>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除category数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除Category",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用CategoryService实现根据ID删除
        categoryService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改Category数据
     * @param category
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改Category",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "Category对象",required = true) Category category,@PathVariable Integer id){
        // 补充id
        category.setId(id);
        //调用CategoryService实现修改Category
        categoryService.update(category);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Category数据
     * @param category
     * @return
     */
    @Operation(summary = "添加Category",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "Category对象",required = true) Category category){
        //调用CategoryService实现添加Category
        try {
            categoryService.add(category);
            return new Result(true,StatusCode.OK,"添加成功",category);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"添加失败",category);
        }

    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @Operation(summary = "Category根据ID查询",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable Integer id){
        //调用CategoryService实现根据ID查询Category
        Category category = categoryService.findById(id);
        return new Result<Category>(true,StatusCode.OK,"查询成功",category);
    }

    /***
     * 查询Category的树形数据
     * @param
     * @return
     */
    @Operation(summary = "查询Category的树形数据",description = "所需权限：")
    @GetMapping("/tree")
    public Result<List<CategoryTree>> findByParentId(){
        // 一级类目对象
        Category t1 = new Category();
        // 二级类目对象
        Category t2 = new Category();
        // 三级类目对象
        Category t3 = new Category();
        // 查询一级类目
        t1.setParentId(0);
        List<Category> t1List = findList(t1).getData();
        // 转换为CategoryTree集合
        List<CategoryTree> tree1 = toCategoryTree(t1List);
        // 遍历一级类目
        for (CategoryTree ct1 : tree1) {
            // 查询二级类目
            t2.setParentId(ct1.getValue());
            List<Category> t2List = findList(t2).getData();
            // 转换为CategoryTree集合
            List<CategoryTree> tree2 = toCategoryTree(t2List);
            // 遍历二级类目
            for (CategoryTree ct2 : tree2) {
                // 查询三级类目，并添加到二级类目中
                t3.setParentId(ct2.getValue());
                ct2.setChildren(toCategoryTree(findList(t3).getData()));
            }
            // 添加一级类目children
            ct1.setChildren(tree2);
        }
        return new Result<List<CategoryTree>>(true,StatusCode.OK,"查询成功",tree1);
    }

    /***
     * 查询Category全部数据
     * @return
     */
    @Operation(summary = "查询所有Category",description = "所需权限：")
    @GetMapping
    public Result<List<Category>> findAll(){
        //调用CategoryService实现查询所有Category
        List<Category> list = categoryService.findAll();
        return new Result<List<Category>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 将Category集合转为CategoryTree集合
     * @param categoryList
     * @return
     */
    public List<CategoryTree> toCategoryTree(List<Category> categoryList){
        // 将Category转为CategoryTree
        List<CategoryTree> list = new ArrayList<>();
        CategoryTree ct = null;
        for (Category category : categoryList) {
            ct = new CategoryTree();
            ct.setValue(category.getId());
            ct.setLabel(category.getName());
            list.add(ct);
        }
        return list;
    }

}
